package com.lzk.order.service.impl;


import com.lzk.basic.service.impl.BaseServiceImpl;
import com.lzk.basic.util.PageList;
import com.lzk.order.domain.OrderPetAcquisition;
import com.lzk.order.dto.OrderDto;
import com.lzk.order.mapper.OrderPetAcquisitionMapper;
import com.lzk.order.query.OrderPetAcquisitionQuery;
import com.lzk.order.service.IOrderPetAcquisitionService;
import com.lzk.org.domain.Employee;
import com.lzk.org.domain.Shop;
import com.lzk.org.mapper.EmployeeMapper;
import com.lzk.org.mapper.ShopMapper;
import com.lzk.pet.domain.Pet;
import com.lzk.pet.domain.SearchMasterMsg;
import com.lzk.pet.mapper.PetMapper;
import com.lzk.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private PetMapper petMapper;
    /**
     * 根据当前登录人来获取分页信息
     * @param query
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> queryData(OrderPetAcquisitionQuery query, Long id) {
        //根据登录id查询当前员工信息
        Employee employee = employeeMapper.loadByLogininfoId(id);
        //判断登录员工是那种角色
        Long shop_id = employee.getShop_id();
        //店铺人员
        if(shop_id!=null){
            Shop shop = shopMapper.loadById(shop_id);
            //判断是否为店员
            if(!shop.getAdmin_id().equals(employee.getId())){
                query.setEmp_id(employee.getId());
            }
            //为店长
            query.setShop_id(shop_id);
        }
        //平台管理员
        return super.queryData(query);
    }

    /**
     * 订单确认业务
     * @param dto
     */
    @Override
    @Transactional
    public void confirm(OrderDto dto) {
        Long orderId = dto.getId();
        Integer paytype = dto.getPaytype();
        BigDecimal money = dto.getMoney();

        //1.修改订单：state = 1，money，payType
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(orderId);
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPaytype(paytype);
        orderPetAcquisition.setPrice(money);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        //2.修改寻主消息：state=2，note
        SearchMasterMsg searchMasterMsg =searchMasterMsgMapper.loadById(orderPetAcquisition.getSearch_master_msg_id());
        searchMasterMsg.setState(2);
        searchMasterMsg.setNote("订单结束，交易完成");
        searchMasterMsgMapper.update(searchMasterMsg);

        //3.修改宠物：money
        Pet pet = petMapper.loadById(orderPetAcquisition.getPet_id());
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    /**
     * 取消订单
     * @param id
     */
    @Override
    public void cancel(Long id) {
        // 1. 修改订单状态
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.loadById(id);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);

        // 2. 将寻主消息的shopid=null
        searchMasterMsgMapper.reject(orderPetAcquisition.getSearch_master_msg_id());

        // 3. pet 删除
        petMapper.remove(orderPetAcquisition.getPet_id());
    }

}
